﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hazop
{
    public partial class ChemicalLibraryForm : Form
    {   
        private List<TreeNode> _allChemicalNodes = new List<TreeNode>();
        private DataTable _allChemicals = new DataTable();
        public ChemicalLibraryForm()
        {
            InitializeComponent();
            LoadData();
        }

        private void LoadData()
        {
            _allChemicals = DataAccess.CSVHelper.ImportExcelToDataSet("./Config/Chemical.xls", "化学品库结构").Tables[0];
            foreach (DataRow row in _allChemicals.Rows)
            {
                if(row[1].ToString() != "" && row[1].ToString()!= "化学品名称") {
                    TreeNode node = new TreeNode(row[1].ToString());
                    _allChemicalNodes.Add(node);
                    tvChemical.Nodes.Add(node);
                }
            }
        }

        private void tbxKeyWord_TextChanged(object sender, EventArgs e){
            string key = tbxKeyWord.Text;
            tvChemical.Nodes.Clear();
            foreach (TreeNode node in _allChemicalNodes) {
                if (node.Text.Contains(key)){
                    tvChemical.Nodes.Add(node);
                }
            }
        }

        private void tvChemical_AfterSelect(object sender, TreeViewEventArgs e){
            TreeNode node = tvChemical.SelectedNode;
            foreach (DataRow row in _allChemicals.Rows){
                if(row[1].Equals(node.Text)){
                    lblName.Text = row[1].ToString();
                    lblCAS.Text = row[2].ToString();
                    lblFenzishi.Text = row[3].ToString();
                    lblXiangtai.Text = row[4].ToString();
                    lblSandian.Text = row[5].ToString();
                    lblLEL.Text = row[6].ToString();
                    lblUEL.Text = row[7].ToString();
                    lblZiranwendu.Text = row[8].ToString();
                    lblZuixiaoyinhuoneng.Text = row[9].ToString();
                    lblFodian.Text = row[10].ToString();
                    lblNinggudian.Text = row[11].ToString();
                    lblyetibizhong.Text = row[12].ToString();
                    lblzhengqimidu.Text = row[13].ToString();
                    lblzhengqiya.Text = row[14].ToString();
                    lblbaoluxianzhi.Text = row[15].ToString();
                    lblteshuweihai.Text = row[16].ToString();
                    break;
                }
            }
        }
    }
}
